package pub.guoxin.oops.auth.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import pub.guoxin.oops.common.auth.utils.BCryptPasswordEncoderUtils;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.model.User;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 用于一阶段注册
 * <p>
 * Created by guoxin on 17-11-15.
 */
@Data
public class AuthRegisterVo {

    @NotBlank(message = "手机号不能为空")
    @ApiModelProperty(value = "手机号", required = true)
    private String mobile;

    @NotBlank(message = "密码不能为空")
    @ApiModelProperty(value = "密码", required = true)
    private String password;

    @NotBlank(message = "验证码不能为空")
    @ApiModelProperty(value = "验证码", required = true)
    private String captcha;

    @NotBlank(message = "名字不能为空")
    @ApiModelProperty(value = "名字", required = true)
    private String name;

    @NotNull
    @ApiModelProperty(value = "权限:ROLE_ROOT, ROLE_ADMIN(公司管理员), ROLE_USER(公司子账号),ROLE_VISITOR(游客权限)", required = true)
    private Authority authority;

    @ApiModelProperty(value = "公司Id可以为空")
    private String companyId;

    public User createUser() {
        User user = new User();
        user.init();
        user.setFirstname(this.name);
        user.setMobile(this.mobile);
        user.setEnabled(true);
        user.setLastPasswordResetDate(new Date());
        user.add(this.authority);
        user.setPassword(BCryptPasswordEncoderUtils.encryptPassword(this.password));
        user.setCompanyId(companyId);
        return user;
    }

    public UsernamePasswordAuthenticationToken createUsernamePasswordAuthenticationToken() {
        return new UsernamePasswordAuthenticationToken(this.mobile, this.password);
    }
}
